package com.huashi.dealer.modular.controller.store.balance;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huashi.dealer.core.page.PageFactory;
import com.huashi.dealer.core.pojo.CommonResult;
import com.huashi.dealer.core.util.SaTokenUtil;
import com.huashi.dealer.modular.business.balance.param.BalanceApplyListParam;
import com.huashi.dealer.modular.business.balance.param.BalanceApplyAuditParam;
import com.huashi.dealer.modular.business.balance.service.BalanceService;
import com.huashi.dealer.modular.business.balance.vo.BalanceApplyListVo;
import com.huashi.dealer.modular.business.balance.param.RechargeReviewListParam;
import com.huashi.dealer.modular.business.balance.param.RechargeReviewAuditParam;
import com.huashi.dealer.modular.business.balance.vo.RechargeReviewListVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * Store端余额管理控制器
 * 对应PHP项目的余额管理功能
 *
 * @author 谭陈强
 * @since 2025-08-18
 */
@Slf4j
@RestController
@RequestMapping("/store/balance")
@Tag(name = "Store端余额管理", description = "打款单列表、审核、复核等功能")
public class BalanceController {

    @Resource
    private BalanceService balanceService;

    /**
     * 打款单列表
     */
    @GetMapping("/apply/list")
    @Operation(summary = "打款单列表", description = "获取打款单列表，支持状态筛选、时间范围筛选、用户昵称筛选")
    public CommonResult<Page<BalanceApplyListVo>> getBalanceApplyList(
            @Parameter(description = "查询参数") @Valid BalanceApplyListParam param) {
        
        try {

            Page<BalanceApplyListVo> page = PageFactory.defaultPage();

            Page<BalanceApplyListVo> result = balanceService.getStoreBalanceApplyPage(param, page);
            
            return CommonResult.data(result);
            
        } catch (Exception e) {
            log.error("查询打款单列表失败", e);
            return CommonResult.error("查询打款单列表失败：" + e.getMessage());
        }
    }

    /**
     * 打款单审核（初审）
     */
    @PostMapping("/apply/audit")
    @Operation(summary = "打款单审核（初审）", description = "对打款单进行初审，可以审核通过或驳回")
    public CommonResult<String> auditBalanceApply(@Parameter(description = "审核参数") @Valid @RequestBody BalanceApplyAuditParam param) {
        
        try {
            Long auditorId = SaTokenUtil.getLoginId();
            balanceService.storeAuditBalanceApply(param.getApplyId(), param.getBalanceStatus(), param.getRejectReason(), auditorId);
            return CommonResult.ok("审核成功");
        } catch (Exception e) {
            log.error("打款单审核失败", e);
            return CommonResult.error("审核失败：" + e.getMessage());
        }
    }

    /**
     * 打款单复核（终审）
     */
    @PostMapping("/apply/review")
    @Operation(summary = "打款单复核（终审）", description = "对已通过初审的打款单进行终审，可以复核通过或驳回")
    public CommonResult<String> reviewBalanceApply(@Parameter(description = "复核参数") @Valid @RequestBody BalanceApplyAuditParam param) {
        
        try {
            Long auditorId = SaTokenUtil.getLoginId();
            balanceService.storeReviewBalanceApply(param.getApplyId(), param.getBalanceStatus(), param.getRejectReason(), auditorId);
            return CommonResult.ok("复核成功");
        } catch (Exception e) {
            log.error("打款单复核失败", e);
            return CommonResult.error("复核失败：" + e.getMessage());
        }
    }

    /**
     * 充值审核列表
     */
    @GetMapping("/recharge/list")
    @Operation(summary = "充值审核列表", description = "获取充值审核列表，支持状态筛选、昵称筛选")
    public CommonResult<Page<RechargeReviewListVo>> getRechargeReviewList(
            @Parameter(description = "查询参数") @Valid RechargeReviewListParam param) {
        try {
            Page<RechargeReviewListVo> page = PageFactory.defaultPage();
            Page<RechargeReviewListVo> result = balanceService.getRechargeReviewPage(param, page);
            return CommonResult.data(result);
        } catch (Exception e) {
            return CommonResult.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 充值初审
     */
    @PostMapping("/recharge/initial")
    @Operation(summary = "充值初审", description = "充值初审通过或驳回")
    public CommonResult<String> rechargeInitialReview(@Parameter(description = "初审参数") @Valid @RequestBody RechargeReviewAuditParam param) {
        try {
            Long auditorId = SaTokenUtil.getLoginId();
            balanceService.rechargeInitialReview(param, auditorId);
            return CommonResult.ok("操作成功");
        } catch (Exception e) {
            return CommonResult.error("操作失败：" + e.getMessage());
        }
    }

    /**
     * 充值复核
     */
    @PostMapping("/recharge/review")
    @Operation(summary = "充值复核", description = "充值复核通过或驳回；通过将按账户类型变动余额并记日志")
    public CommonResult<String> rechargeCheckReview(@Parameter(description = "复核参数") @Valid @RequestBody RechargeReviewAuditParam param) {
        try {
            Long auditorId = SaTokenUtil.getLoginId();
            balanceService.rechargeCheckReview(param, auditorId);
            return CommonResult.ok("操作成功");
        } catch (Exception e) {
            return CommonResult.error("操作失败：" + e.getMessage());
        }
    }
}
